package com.jajahome.feature.user.activity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jajahome.R;
import com.jajahome.base.BaseActivity;
import com.jajahome.constant.Constant;
import com.jajahome.model.RebateDetailsModel;
import com.jajahome.model.RebateInfoModel_v2;
import com.jajahome.network.ApiImp;
import com.jajahome.network.HttpUtil;
import com.jajahome.network.RebateDetailReq;
import com.jajahome.util.StringUtil;
import com.jajahome.util.convertutils.ErrorUtil;
import com.squareup.picasso.Picasso;

import java.io.UnsupportedEncodingException;
import java.util.List;

import butterknife.BindView;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class RebateInfoDetailAct extends BaseActivity implements View.OnClickListener {
    public final static String TRADE_NO = "trade_no";
    @BindView(R.id.ibtn_back)
    ImageButton btn_close;
    @BindView(R.id.tv_rebate_price)
    TextView tv_rebate_price;
    @BindView(R.id.image_person)
    CircleImageView iv_person;
    @BindView(R.id.tv_nickname)
    TextView tv_nickname;
    @BindView(R.id.tv_mobile)
    TextView tv_mobile;
    @BindView(R.id.tv_status)
    TextView tv_status;
    @BindView(R.id.tv_trade_no)
    TextView tv_trade_no;
    @BindView(R.id.tv_rebate_order_price)
    TextView tv_rebate_order_price;
    @BindView(R.id.tv_order_time)
    TextView tv_order_time;
    @BindView(R.id.tv_rebate_num)
    TextView tv_num;
    @BindView(R.id.tv_cashback)
    TextView tv_cashback;
    @BindView(R.id.tv_tax)
    TextView tv_tax;
    @BindView(R.id.tv_subsidy)
    TextView tv_subsidy;
    @BindView(R.id.tv_available)
    TextView tv_available;
    @BindView(R.id.btn_pay)
    Button btn_pay;
    @BindView(R.id.ll_subsidy)
    LinearLayout ll;
    private String trade_no;
    private Context mContext;
    private String totalPrice;

    @Override
    protected int getViewId() {
        return R.layout.act_rebate_info_detail;
    }

    @Override
    protected void initEvent() {
        trade_no = getIntent().getStringExtra(TRADE_NO);
        totalPrice = getIntent().getStringExtra(CashAct.CASHACT);
        btn_close.setOnClickListener(this);
        btn_pay.setOnClickListener(this);
        mContext = this;
        getData();
    }

    private void getData() {
        RebateDetailReq.ContentBean contentBean = new RebateDetailReq.ContentBean();
        contentBean.setTrade_no(trade_no);
        RebateDetailReq req = new RebateDetailReq();
        req.setContent(contentBean);
        req.setCmd(Constant.REBATE_ORDER_DETAILS);
        Gson gson = new Gson();
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), gson.toJson(req));
        mSubscription = ApiImp.get().rebate_order_details(requestBody, HttpUtil.getSession(mContext)).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).
                subscribe(new Observer<RebateDetailsModel>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        ErrorUtil.showMessage(mContext, e);
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(RebateDetailsModel model) {
                        if (model != null) {
                            setData(model);
                        }
                    }
                });
    }

    private void setData(RebateDetailsModel result) {
        try {
            tv_rebate_price.setText("¥" + String.valueOf(result.getRebateDetail().getAvailable()));
            if (!StringUtil.isEmpty(result.getAvatar())) {
                Picasso.with(this).load(result.getAvatar()).error(R.mipmap.ic_holder_header_big).placeholder(R.mipmap.ic_holder_header_big).into(iv_person);
            }
            //下单用户昵称小于5只获取前两个字节数，大于5前两字节后两字节,小于3字节全显示
            String nickname = result.getRebateDetail().getNickname().replaceAll(" ", "");
            int leng = StringUtil.getStringByteLenths(nickname);
            if (leng < 3) {
                tv_nickname.setText(nickname);
            } else if (leng < 5) {
                tv_nickname.setText(StringUtil.substringByte(nickname, 0, 2) + "*****");
            } else if (leng > 5) {
                tv_nickname.setText(StringUtil.substringByte(nickname, 0, 2) + "*****" + StringUtil.substringByte(nickname, leng - 1, leng));
            }
            //手机号码只取11位只获取前三个跟后三个
            String mobile = result.getRebateDetail().getMobile();
            if (mobile.length()<11){
                tv_mobile.setText(mobile.substring(0, 3) + "*****" + mobile.substring(mobile.length()-3, mobile.length()));
            }else {
                tv_mobile.setText(mobile.substring(0, 3) + "*****" + mobile.substring(8, 11));
            }
            if (result.getRebateDetail().getStatus() == 3) {
                tv_status.setText("交易完成");
            }
            tv_trade_no.setText(result.getRebateDetail().getTrade_no());
            tv_rebate_order_price.setText("¥" + result.getRebateDetail().getPrice());
            tv_order_time.setText(result.getRebateDetail().getOrder_time());
            tv_num.setText(String.valueOf(result.getRebateDetail().getNum()));
            tv_cashback.setText("¥" + result.getRebateDetail().getCashback());
            tv_tax.setText("- ¥" + String.valueOf(result.getRebateDetail().getTax()));
            if (result.getRebateDetail().getSubsidy() == 0) {
                ll.setVisibility(View.GONE);
            } else {
                tv_subsidy.setText("+ ¥" + String.valueOf(result.getRebateDetail().getSubsidy()));
            }
            tv_available.setText("¥" + String.valueOf(result.getRebateDetail().getAvailable()));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.btn_pay:
                Intent intent = new Intent(mContext, CashAct.class);
                intent.putExtra(CashAct.CASHACT, totalPrice);
                startActivity(intent);
                finish();
                break;
        }
    }
}
